<?php
/*
 * FastSHL                              | Universal Syntax HighLighter |
 * ---------------------------------------------------------------------

   Copyright (C) 2002-2003  Juraj 'hvge' Durech

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
   
 * ---------------------------------------------------------------------
 */
function get_string_source($var)
{
	if(is_int($var))
		return $var;

	// TODO use double pass for backslashes detection
	$exp = null;
	$escaped = false;
	for($i=0; $i<strlen($var); $i++)
	{
		$add = null;
		$ch = $var[$i];
		switch($ch)
		{
			case "'": $escaped = true; break;
			case "\n": $add='n'; break;
			case "\t": $add='t'; break;
			case "\r": $add='r'; break;
			case "\$": $add='$'; break;
			case '"': $add='"'; break;
			case "\\": $add="\\"; break;
			default:
				if(( $ch < ' ' ) || ( $ch > '~' ))
					$add='x'.bin2hex($ch);
				break;
		}
		if($add!=null) {
			$exp.="\\".$add;
			$escaped = true; 
		} else {
			$exp.=$ch;
		}
	}
	return $escaped ? '"'.$exp.'"' : "'$exp'";
}

function get_array_source($var)
{
	
	if(is_int($var) || is_float($var))
		return $var;
	
	if($var == null)
		return "null";
		
	if(is_string($var))
	{
		return get_string_source($var);
	}
	if(is_array($var))
	{
		$exp="array(";
		foreach($var as $key => $value)
			$exp.=get_string_source($key)."=>".get_array_source($value).",";
		$exp.=")";
		return $exp;
	}
}

function get_var_source($varname,$var) 
{ 
	return "\$".$varname."=".get_array_source($var).";\n"; 
}

function get_fnc_source($fncname,$param=null)
{
	return "function $fncname ($param)\n{\n";
}


?>